﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeBehind="OrderList.aspx.cs" Inherits="Project_Udrinkwater.Empoyee_Management.OrderList" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
                            <dx:ASPxGridViewExporter ID="gridExport" runat="server" 
        GridViewID="grid" PaperKind="A4">
                                <Styles>
                                    <Default Font-Names="Angsana New">
                                    </Default>
                                </Styles>
                            </dx:ASPxGridViewExporter>
                            <asp:Panel ID="Panel2" runat="server">
                                <table style="width: 100%">
                                    <tr>
                                        <td style="width: 393px">
                                            <dx:ASPxButton ID="ASPxButton1" runat="server" Text="กลับหน้าการขาย" 
                                                onclick="ASPxButton1_Click" Width="120px">
                                            </dx:ASPxButton>
                                        </td>
                                        <td align="right" style="width: 609px">
                                            <dx:ASPxButton ID="btnPdfExport" runat="server" OnClick="btnPdfExport_Click" 
                                                Text="พิมพ์ใบเสร็จ" UseSubmitBehavior="False" Width="150px" />
                                        </td>
                                        <td align="right">
                                            <dx:ASPxButton ID="btnXlsExport" runat="server" OnClick="btnXlsExport_Click" 
                                                Text="พิมพ์ใบเสร็จ XLS" UseSubmitBehavior="False" Width="150px" 
                                                Visible="False" />
                                        </td>
                                    </tr>
                                </table>
    </asp:Panel>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Panel ID="Panel1" runat="server">
                <fieldset>
                <legend>การขาย</legend>
                <table style="width: 100%">
                    <tr>
                        <td align="right" style="width: 199px">
                            <asp:Label ID="Label1" runat="server" Text="เลขที่เอกสาร:"></asp:Label>
                        </td>
                        <td style="width: 254px">
                            <dx:ASPxTextBox ID="txtDocNo" runat="server" ReadOnly="True" Width="250px">
                            </dx:ASPxTextBox>
                        </td>
                        <td align="right" style="width: 167px">
                            <asp:Label ID="Label3" runat="server" Text="วันที่เอกสาร:"></asp:Label>
                        </td>
                        <td>
                            <dx:ASPxTextBox ID="txtdocDate" runat="server" ReadOnly="True" Width="250px">
                            </dx:ASPxTextBox>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 199px">
                            &nbsp;</td>
                        <td style="width: 254px">
                            &nbsp;</td>
                        <td align="right" style="width: 167px">
                            &nbsp;</td>
                        <td>
                            &nbsp;</td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 199px">
                            <asp:Label ID="Label2" runat="server" Text="วันกำหนดส่ง:"></asp:Label>
                        </td>
                        <td style="width: 254px">
                            <dx:ASPxTextBox ID="txtDeadline" runat="server" ReadOnly="True" Width="250px">
                            </dx:ASPxTextBox>
                        </td>
                        <td align="right" style="width: 167px">
                            <asp:Label ID="Label4" runat="server" Text="พนักงานทำรายการ:"></asp:Label>
                        </td>
                        <td>
                            <dx:ASPxComboBox ID="cmbEmp" runat="server" DataSourceID="SqlEmp" 
                                ReadOnly="True" TextField="Name" ValueField="Employee_ID" Width="250px" 
                                TextFormatString="{0} {1}">
                                <Columns>
                                    <dx:ListBoxColumn FieldName="Name" />
                                    <dx:ListBoxColumn FieldName="Department_Name" />
                                </Columns>
                            </dx:ASPxComboBox>
                        </td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 199px">
                            &nbsp;</td>
                        <td style="width: 254px">
                            &nbsp;</td>
                        <td align="right" style="width: 167px">
                            &nbsp;</td>
                        <td>
                            &nbsp;</td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 199px">
                            <asp:Label ID="Label5" runat="server" Text="ชื่อลูกค้า:"></asp:Label>
                        </td>
                        <td style="width: 254px">
                            <dx:ASPxComboBox ID="cmbCustomer" runat="server" DataSourceID="SqlCustomer" 
                                ReadOnly="True" TextField="Name" ValueField="Customer_ID" Width="250px" 
                                TextFormatString="{1}">
                                <Columns>
                                    <dx:ListBoxColumn FieldName="Customer_ID" />
                                    <dx:ListBoxColumn FieldName="Name" />
                                </Columns>
                            </dx:ASPxComboBox>
                        </td>
                        <td align="right" style="width: 167px">
                            &nbsp;</td>
                        <td>
                            &nbsp;</td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 199px">
                            &nbsp;</td>
                        <td style="width: 254px">
                            &nbsp;</td>
                        <td align="right" style="width: 167px">
                            &nbsp;</td>
                        <td>
                            &nbsp;</td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 199px">
                            <asp:Label ID="Label6" runat="server" Text="ที่อยู่จัดส่งสินค้า:"></asp:Label>
                        </td>
                        <td style="width: 254px">
                            <dx:ASPxMemo ID="txtAddress" runat="server" Height="88px" Width="250px">
                            </dx:ASPxMemo>
                        </td>
                        <td align="right" style="width: 167px">
                            &nbsp;</td>
                        <td align="right">
                            <dx:ASPxButton ID="btnXlsxExport" runat="server" OnClick="btnXlsxExport_Click" 
                                Text="Export to XLSX" UseSubmitBehavior="False" Visible="False" />
                            <dx:ASPxButton ID="btnRtfExport" runat="server" OnClick="btnRtfExport_Click" 
                                Text="Export to RTF" UseSubmitBehavior="False" Visible="False" />
                            <dx:ASPxButton ID="btnCsvExport" runat="server" OnClick="btnCsvExport_Click" 
                                Text="Export to CSV" UseSubmitBehavior="False" Visible="False" />
                        </td>
                    </tr>
                </table>
                 </fieldset>
            </asp:Panel>
            <asp:Panel ID="Panel3" runat="server">
                <table style="width: 100%">
                    <tr>
                        <td style="width: 203px">
                            &nbsp;</td>
                        <td>
                            &nbsp;</td>
                    </tr>
                </table>
            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
        <ContentTemplate>
             <fieldset>
             <legend>ตารางรายการขาย</legend>
                 <table style="width: 100%">
                     <tr>
                         <td style="width: 118px">
                             &nbsp;</td>
                         <td>
                             &nbsp;</td>
                         <td align="right">
                             &nbsp;</td>
                     </tr>
                 </table>
            <dx:ASPxGridView ID="grid" runat="server" AutoGenerateColumns="False" 
                ClientInstanceName="grid" DataSourceID="SqlOderList" KeyFieldName="id" 
                onbeforeperformdataselect="ASPxGridView2_BeforePerformDataSelect" Width="100%" 
                     onhtmleditformcreated="grid_HtmlEditFormCreated" 
                     onrowinserting="grid_RowInserting" onrowupdating="grid_RowUpdating" 
                     oncustomsummarycalculate="grid_CustomSummaryCalculate" 
                     onhtmlfootercellprepared="grid_HtmlFooterCellPrepared">
                <Columns>
                    <dx:GridViewCommandColumn Caption="#" VisibleIndex="10">
                        <EditButton Text="แก้ไข" Visible="True">
                        </EditButton>
                        <NewButton Text="เพิ่ม" Visible="True">
                        </NewButton>
                        <DeleteButton Text="ลบ" Visible="True">
                        </DeleteButton>
                        <ClearFilterButton Visible="True">
                        </ClearFilterButton>
                    </dx:GridViewCommandColumn>
                    <dx:GridViewDataTextColumn FieldName="Order_ID" ReadOnly="True" Visible="False" 
                        VisibleIndex="2">
                        <EditFormSettings Visible="False" />
                    </dx:GridViewDataTextColumn>
                    <dx:GridViewDataTextColumn Caption="เลขที่เอกสาร" FieldName="Order_doc_NO" 
                        ReadOnly="True" Visible="False" VisibleIndex="1" Width="200px">
                        <EditFormSettings Visible="False" />
                    </dx:GridViewDataTextColumn>
                    <dx:GridViewDataTextColumn FieldName="id" ReadOnly="True" Visible="False" 
                        VisibleIndex="3">
                        <EditFormSettings Visible="False" />
                    </dx:GridViewDataTextColumn>
                    <dx:GridViewDataComboBoxColumn Caption="ประเภทการขาย" 
                        FieldName="Order_Category_ID" VisibleIndex="5" Width="100px">
                        <PropertiesComboBox DataSourceID="SqlOrderCateAll" 
                            TextField="Order_Category_Name" ValueField="Order_Category_ID" 
                            ValueType="System.Int32">
                        </PropertiesComboBox>
                    </dx:GridViewDataComboBoxColumn>
                    <dx:GridViewDataComboBoxColumn Caption="รหัสสินค้า" FieldName="Product_ID" 
                        VisibleIndex="4">
                        <PropertiesComboBox DataSourceID="SqlProductAll" TextField="Product_Name" 
                            ValueField="Product_ID">
                            <ClientSideEvents SelectedIndexChanged="function(s, e) {OnCategory1Changed(s); }" />
                        </PropertiesComboBox>
                    </dx:GridViewDataComboBoxColumn>
                    <dx:GridViewDataTextColumn Caption="จำนวน" FieldName="Amount" VisibleIndex="7">
                    </dx:GridViewDataTextColumn>
                    <dx:GridViewDataTextColumn Caption="ราคา" FieldName="Price_Unit" 
                        VisibleIndex="6">
                    </dx:GridViewDataTextColumn>
                    <dx:GridViewDataTextColumn Caption="ส่วนลด" FieldName="Discount" 
                        VisibleIndex="8">
                    </dx:GridViewDataTextColumn>
                    <dx:GridViewDataTextColumn Caption="ลำดับ" FieldName="Row" VisibleIndex="0">
                        <EditFormSettings Visible="False" />
                    </dx:GridViewDataTextColumn>
                    <dx:GridViewDataTextColumn Caption="รวม" FieldName="Total" 
                        UnboundType="Decimal" VisibleIndex="9">
                        <EditFormSettings Visible="False" />
                    </dx:GridViewDataTextColumn>
                </Columns>
                <SettingsEditing Mode="EditForm" />
                <Settings ShowFilterRow="True" ShowFooter="True" />
                <TotalSummary>
                    <dx:ASPxSummaryItem DisplayFormat="รวม {0:#,###.00}" FieldName="Total" 
                        ShowInColumn="Total" ShowInGroupFooterColumn="Total" SummaryType="Sum" />
                </TotalSummary>
                <Templates>
                    <EditForm>
                         <table style="width: 100%">
                                     <tr>
                                         <td align="right" style="width: 243px; height: 27px;">
                                             <dx:ASPxLabel ID="ASPxLabel1" runat="server" Text="สินค้า:">
                                             </dx:ASPxLabel>
                                         </td>
                                         <td style="height: 27px">
                                             <dx:ASPxComboBox ID="cmbProduct" runat="server" AutoPostBack="True" 
                                                 DataSourceID="SqlProductAll" 
                                                 onselectedindexchanged="cmbProduct_SelectedIndexChanged" 
                                                 TextField="Product_Name" ValueField="Product_ID" Width="200px">
                                             </dx:ASPxComboBox>
                                         </td>
                                         <td style="width: 181px; height: 27px;">
                                             <dx:ASPxLabel ID="ASPxLabel2" runat="server" Text="ประเภทการขาย:">
                                             </dx:ASPxLabel>
                                         </td>
                                         <td style="width: 214px; height: 27px;">
                                             <dx:ASPxComboBox ID="cmbOrdercate" runat="server" AutoPostBack="True" 
                                                 DataSourceID="SqlOrdercate" 
                                                 onselectedindexchanged="cmbOrdercate_SelectedIndexChanged" 
                                                 TextField="Order_Category_Name" ValueField="Order_Category_ID" 
                                                 Width="200px" ValueType="System.Int32">
                                             </dx:ASPxComboBox>
                                         </td>
                                         <td style="width: 181px; height: 27px;">
                                             &nbsp;</td>
                                     </tr>
                                     <tr>
                                         <td align="right" style="width: 243px">
                                             <dx:ASPxLabel ID="ASPxLabel3" runat="server" Text="ราคา">
                                             </dx:ASPxLabel>
                                         </td>
                                         <td>
                                             <dx:ASPxComboBox ID="cmbPrice" runat="server" DataSourceID="SqlPrice" 
                                                 TextField="Price" ValueField="Price" ValueType="System.Int32" Width="200px">
                                             </dx:ASPxComboBox>
                                         </td>
                                         <td style="width: 181px">
                                             <dx:ASPxLabel ID="ASPxLabel4" runat="server" Text="จำนวน:">
                                             </dx:ASPxLabel>
                                         </td>
                                         <td style="width: 181px" colspan="2">
                                             <dx:ASPxTextBox ID="txtAmount" runat="server" Width="200px">
                                             </dx:ASPxTextBox>
                                         </td>
                                     </tr>
                                     <tr>
                                         <td align="right" style="width: 243px">
                                             <dx:ASPxLabel ID="ASPxLabel5" runat="server" Text="ส่วนลด:">
                                             </dx:ASPxLabel>
                                         </td>
                                         <td>
                                             <dx:ASPxTextBox ID="txtDiscount" runat="server" Width="200px">
                                             </dx:ASPxTextBox>
                                         </td>
                                         <td style="width: 181px">
                                             &nbsp;</td>
                                         <td style="width: 214px">
                                             &nbsp;</td>
                                         <td style="width: 181px">
                                             &nbsp;</td>
                                     </tr>
                                 </table>
                        <div style="text-align: right; padding: 2px 2px 2px 2px">
                    <dx:ASPxGridViewTemplateReplacement ID="UpdateButton" ReplacementType="EditFormUpdateButton"
                        runat="server">
                    </dx:ASPxGridViewTemplateReplacement>
                    <dx:ASPxGridViewTemplateReplacement ID="CancelButton" ReplacementType="EditFormCancelButton"
                        runat="server">
                    </dx:ASPxGridViewTemplateReplacement>
                </div>
                    </EditForm>
                </Templates>
            </dx:ASPxGridView>
            </fieldset>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:SqlDataSource ID="SqlEmp" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
        SelectCommand="SELECT TB_Employee.Employee_ID, TB_Employee.FirstName + ' ' + TB_Employee.LastName AS Name, TB_Department.Department_Name FROM TB_Employee INNER JOIN TB_Department ON TB_Employee.Departmernt_ID = TB_Department.Department_ID
Where  (TB_Employee.Employee_ID = @Employee_ID)" onselecting="SqlEmp_Selecting">
        <SelectParameters>
            <asp:Parameter Name="Employee_ID" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlCustomer" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
        
        SelectCommand="SELECT TB_Customer.Firstname + '  ' + TB_Customer.Lastname AS Name, TB_Customer.Customer_ID, TB_Customer.Adress, TB_Customer.DISTRICT_ID, TB_Customer.AMPHUR_ID, TB_Customer.PROVINCE_ID, TB_Customer.Postcode, TB_Customer.Phone, TB_Customer.Fax, TB_District.DISTRICT_NAME, TB_Amphur.AMPHUR_NAME, TB_Province.PROVINCE_NAME FROM TB_Customer INNER JOIN TB_District ON TB_Customer.DISTRICT_ID = TB_District.DISTRICT_ID INNER JOIN TB_Amphur ON TB_Customer.AMPHUR_ID = TB_Amphur.AMPHUR_ID AND TB_District.AMPHUR_ID = TB_Amphur.AMPHUR_ID INNER JOIN TB_Province ON TB_Customer.PROVINCE_ID = TB_Province.PROVINCE_ID AND TB_District.PROVINCE_ID = TB_Province.PROVINCE_ID AND TB_Amphur.PROVINCE_ID = TB_Province.PROVINCE_ID WHERE (TB_Customer.Customer_ID = @Customer_ID)" 
        onselecting="SqlCustomer_Selecting">
        <SelectParameters>
            <asp:SessionParameter Name="Customer_ID" SessionField="sCustomer_ID" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlOrder" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
        
        SelectCommand="SELECT Order_ID, Order_doc_NO, Order_Date_Time, Credit, Deadlines_Date, Employee_ID, Customer_ID FROM TB_Order
WHERE (Order_ID = @Order_ID)" 
        DeleteCommand="DELETE FROM TB_Order WHERE (Order_ID = @Order_ID)" 
        InsertCommand="INSERT INTO TB_Order(Order_Date_Time, Credit, Deadlines_Date, Employee_ID, Customer_ID) VALUES (@Order_Date_Time, @Credit, @Deadlines_Date, @Employee_ID, @Customer_ID)" 
        
        
        
        UpdateCommand="UPDATE TB_Order SET Order_Date_Time = @Order_Date_Time  , Credit = @Credit , Deadlines_Date = @Deadlines_Date  , Employee_ID = @Employee_ID , Customer_ID = @Customer_ID   WHERE (Order_ID = @Order_ID)" 
        onselecting="SqlOrder_Selecting" onupdating="SqlOrder_Updating">
        <DeleteParameters>
            <asp:Parameter Name="Order_ID" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Order_Date_Time" />
            <asp:Parameter Name="Credit" />
            <asp:Parameter Name="Deadlines_Date" />
            <asp:Parameter Name="Employee_ID" />
            <asp:Parameter Name="Customer_ID" />
        </InsertParameters>
        <SelectParameters>
            <asp:SessionParameter Name="Order_ID" SessionField="Order_ID" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Order_Date_Time" />
            <asp:Parameter Name="Credit" />
            <asp:Parameter Name="Deadlines_Date" />
            <asp:Parameter Name="Employee_ID" />
            <asp:Parameter Name="Customer_ID" />
            <asp:Parameter Name="Order_ID" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlOderList" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
        DeleteCommand="DELETE FROM TB_Order_List WHERE (id = @id)" 
        InsertCommand="INSERT INTO TB_Order_List(Order_ID, Order_Category_ID, Product_ID, Amount, Price_Unit, Discount) VALUES (@Order_ID, @Order_Category_ID, @Product_ID, @Amount, @Price_Unit, @Discount);" 
        
        
        SelectCommand="SELECT TB_Order.Order_ID, TB_Order.Order_doc_NO,ROW_NUMBER() OVER(ORDER BY TB_Order.Order_ID DESC) AS Row, TB_Order_List.id, TB_Order_List.Order_Category_ID, TB_Order_List.Product_ID, TB_Order_List.Price_Unit, TB_Order_List.Amount, TB_Order_List.Discount , (TB_Order_List.Price_Unit *TB_Order_List.Amount-TB_Order_List.Discount) AS Total FROM TB_Order INNER JOIN TB_Order_List ON TB_Order.Order_ID = TB_Order_List.Order_ID INNER JOIN TB_Order_Category ON TB_Order_List.Order_Category_ID = TB_Order_Category.Order_Category_ID  WHERE (TB_Order.Order_ID = @Order_ID)" 
        
        
        
        
        
        
        
        UpdateCommand="UPDATE TB_Order_List SET Order_Category_ID = @Order_Category_ID, Product_ID = @Product_ID, Amount = @Amount, Price_Unit =@Price_Unit , Discount =@Discount  WHERE (id = @id)">
        <DeleteParameters>
            <asp:Parameter Name="id" />
        </DeleteParameters>
        <InsertParameters>
            <asp:SessionParameter Name="Order_ID" SessionField="Order_ID" />
            <asp:Parameter Name="Order_Category_ID" />
            <asp:Parameter Name="Product_ID" />
            <asp:Parameter Name="Amount" />
            <asp:Parameter Name="Price_Unit" />
            <asp:Parameter Name="Discount" />
        </InsertParameters>
        <SelectParameters>
            <asp:SessionParameter Name="Order_ID" SessionField="Order_ID" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Order_Category_ID" />
            <asp:Parameter Name="Product_ID" />
            <asp:Parameter Name="Amount" />
            <asp:Parameter Name="Price_Unit" />
            <asp:Parameter Name="Discount" />
            <asp:Parameter Name="id" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlOrdercate" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
                 SelectCommand="SELECT Order_Category_ID, Order_Category_Name, Price FROM TB_Order_Category WHERE (Product_ID = @Product_ID)">
                 <SelectParameters>
                     <asp:SessionParameter  Name="Product_ID" 
                         SessionField="sProduct_ID" />
                 </SelectParameters>
             </asp:SqlDataSource>
             <asp:SqlDataSource ID="SqlOrdercateAll" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
                 SelectCommand="SELECT [Order_Category_ID], [Order_Category_Name] FROM [TB_Order_Category]">
             </asp:SqlDataSource>
             <asp:SqlDataSource ID="SqlPrice" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" SelectCommand="SELECT        Order_Category_ID, Order_Category_Name, Price
FROM            TB_Order_Category
WHERE        (Order_Category_ID= @Order_Category_ID)">
                 <SelectParameters>
                     <asp:SessionParameter Name="Order_Category_ID" SessionField="sOrderCatID" />
                 </SelectParameters>
             </asp:SqlDataSource>
             <asp:SqlDataSource ID="SqlProductAll" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
                 SelectCommand="SELECT * FROM [TB_Product]"></asp:SqlDataSource>
</asp:Content>
